//
//  SAShopListSelectController.m
//  ShopAssistant
//
//  Created by FangGeek on 2017/11/20.
//  Copyright © 2017年 hyxt. All rights reserved.
//

#import "SAShopListSelectController.h"
#import "SAShopListCell.h"
#import "SAHomeController.h"
#import "SALeftMenuController.h"
#import "XLSlideMenu.h"

@interface SAShopListSelectController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SAShopListSelectController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"SAShopListCell" bundle:nil] forCellReuseIdentifier:@"SAShopListCell"];
    
    self.title = @"我的企业";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(popViewControllerAnimated:)];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SAShopListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAShopListCell"];
    cell.dict = self.datas[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *tempDict = self.datas[indexPath.row];
    
    [MBProgressHUD showMessage:@"请稍候..."];
    [SAHttpApi requestGetUserInfoWithPhone:[SAUserDefaults objectForKey:@"SAPhone"] WithShopId:tempDict[@"shopId"] WithComeBack:^(NSDictionary *object, NSError *error) {
        if (!error)
        {
            if ([object[@"status"] integerValue] == 1)
            {
                SAUserInfoModel *model = [SAUserInfoModel mj_objectWithKeyValues:object[@"content"]];
                [SAUserInfoManager sharedSAUserInfoManager].model = model;
                [SAUserInfoManager sharedSAUserInfoManager].model._id = object[@"content"][@"id"];
                [SAUserDefaults setObject:tempDict[@"shopId"] forKey:@"SAShopId"];
                [[SAUserInfoManager sharedSAUserInfoManager] saveLoginInfo];
                
                [SAHttpApi requestGetHTTPURLWithNumber:@"1" WithEmpId:[SAUserInfoManager sharedSAUserInfoManager].model._id WithAppTo:[SAUserInfoManager sharedSAUserInfoManager].model.identityCollect WithComeBack:^(NSDictionary *object, NSError *error) {
                    if (!error)
                    {
                        
                        if ([object[@"status"] integerValue] == 1)
                        {
                            [MBProgressHUD hideHUD];
                            
                            SAHomeController *vc = [[SAHomeController alloc] init];
                            vc.urlString = object[@"content"];
                            SANavigationController *nav = [[SANavigationController alloc] initWithRootViewController:vc];
                            
                            //左侧菜单
                            //左侧菜单
                            SALeftMenuController *leftVC = [[SALeftMenuController alloc] init];
                            //创建滑动菜单
                            XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:nav];
                            //设置左右菜单
                            slideMenu.leftViewController = leftVC;
                            
                            [UIApplication sharedApplication].keyWindow.rootViewController = slideMenu;
                        }
                        else
                        {
                            [MBProgressHUD hideHUD];
                            [MBProgressHUD showError:object[@"msg"]];
                            
                        }
                    }
                    else
                    {
                        [MBProgressHUD hideHUD];
                        [UIAlertView showWithTitle:@"提示" message:@"请检查网络设置,稍后再试.." cancelButtonTitle:nil otherButtonTitles:@[@"我知道了"] tapBlock:nil];
                    }
                }];
                
            }
            else
            {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:object[@"msg"]];
                
            }
        }
        else
        {
            [MBProgressHUD hideHUD];
            [UIAlertView showWithTitle:@"提示" message:@"请检查网络设置,稍后再试.." cancelButtonTitle:nil otherButtonTitles:@[@"我知道了"] tapBlock:nil];
            
        }
    }];
    
    
    
}

@end
